package com.seek.inter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSONObject;
import com.seek.cache.CacheUtil;
import com.seek.common.handler.SpringContextHolder;
import com.seek.model.dto.ResultInfo;
import com.seek.model.entity.TConfig;
import com.seek.service.ConfigService;
import com.seek.util.JsonUtil;

/**
 * 和系统相关的接口服务
 *
 */
public class SysInter {
	
	private ConfigService configService = (ConfigService)SpringContextHolder.getBean("configService");
	
	/**
	 * 加载系统参数
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ResultInfo getConfig(HttpServletRequest request, JSONObject reqJson) throws Exception {
		ResultInfo result = new ResultInfo();
		String companyId = reqJson.getString("companyId");
	
		// 默认从缓存读取，在后台更新后清除缓存
		Map<String,Object> ret = JsonUtil.fromJson(CacheUtil.getRedisData("SYSCONFIG_"+companyId));
		if (ret == null || ret.size() < 1){
			ret = new HashMap<String,Object>();
			String sql = "select * from t_config where company_id=? and is_used=1";
			List<TConfig> data = configService.findBySql(sql, new Object[]{companyId});
			for (TConfig en : data){
				if ("INTERST_DAY".equals(en.getType()))
					ret.put("interestDay", en.getValue());
				if ("HANDLEFEE_DAY".equals(en.getType()))
					ret.put("magFeeDay", en.getValue());	
				if ("LATEFEE_DAY".equals(en.getType()))
					ret.put("lateFeeDay", en.getValue());
				if ("DAYS".equals(en.getType()))
					ret.put("days", en.getValue());
				if ("LATEFEE_DAY2".equals(en.getType()))
					ret.put("lateFeeDay2", en.getValue());
			}
			CacheUtil.putRedisData("SYSCONFIG_"+companyId, JsonUtil.toJson(ret));
		}
		
		result.setStatus(0);
		result.setMessage("成功");
		result.setBody(ret);
		return result;
	}
	
}
